//
//  ThirdViewController.m
//  XDCommonApp
//
//  Created by XD-XY on 2/12/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//

#import "ThirdViewController.h"
#import "trolleyCell.h"
#import "ShopTrolley.h"
#import "SmallButton.h"
#import "XDTools.h"
#import "XDHeader.h"
#import "WriteOrderViewController.h"
//#import "goodDetailViewController.h"
@interface ThirdViewController ()
{
    float total;
}
@end

@implementation ThirdViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.dataArrary = [NSMutableArray arrayWithCapacity:3];
        self.indexArrary = [NSMutableArray arrayWithCapacity:3];
        
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    [self getShopTrolleyListDatas];
}
-(void)viewWillDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.titleLabel.text = @"购物车";
    self.titleLabel.textColor = [UIColor whiteColor];
    self.leftBtn.hidden = YES;
   
    
    if (IOS7) {
        self.navigationBarBg.image = [UIImage imageNamed:@"redIOS7"];
    }else{
        self.navigationBarBg.image = [UIImage imageNamed:@"redIOS6"];
    }
    
    self.titleLabel.textColor = [UIColor whiteColor];
    rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitle:@"删除" forState:UIControlStateNormal];
    [rightButton setTintColor:[UIColor whiteColor]];
    rightButton.hidden = YES;
    [rightButton setFrame:CGRectMake(520/2.0f, 13+aHeight, 60, 25)];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [rightButton addTarget:self action:@selector(deleteShop) forControlEvents:UIControlEventTouchUpInside];
    
    [self.navigationBarView addSubview:rightButton];
    
   
    
    
    
    [self initShopTableView];
    
    
    
    // Do any additional setup after loading the view.
}
-(void)initShopTableView{
    self.shopTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.contentView.frame.size.height) style:UITableViewStylePlain];
    _shopTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _shopTableview.delegate = self;
    _shopTableview.dataSource = self;
    //    _shopTableview.rowHeight = 133;
    _shopTableview.showsVerticalScrollIndicator = NO;
    
    _shopTableview.backgroundColor = RGBA(252, 251, 249, 1.0);
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 30)];
    footView.backgroundColor = RGBA(252, 251, 249, 1.0);
    _shopTableview.tableFooterView = footView;
    
    [self.contentView addSubview:_shopTableview];
    
    noDatasView = [[UIImageView alloc] initWithFrame:CGRectMake((UI_SCREEN_WIDTH-138)/2, (UI_MAINSCREEN_HEIGHT - UI_NAVIGATION_BAR_HEIGHT - 88)/2, 138, 78)];
    noDatasView.image = [UIImage imageNamed:@"refreshFailed"];
    [self.contentView addSubview:noDatasView];
    noDatasView.hidden = YES;
    
    //初始化下面结算view
    jiesuanView = [[UIView alloc] initWithFrame:CGRectMake(0, self.contentView.frame.size.height-97, UI_SCREEN_WIDTH, 48)];
    jiesuanView.backgroundColor = [UIColor blackColor];
    jiesuanView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.7];
    jiesuanView.hidden = YES;
    [self.contentView addSubview:jiesuanView];
    
    
    window = [[[UIApplication sharedApplication] delegate] window];
    toumingView = [[UIView alloc] initWithFrame:window.frame];
    toumingView.backgroundColor = [UIColor blackColor];
    toumingView.alpha = .7;
    
    
    allImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 15.5, 19, 17)];
    allImageView.image = [UIImage imageNamed:@"all_selected"];
    [jiesuanView addSubview:allImageView];
    
    UILabel *allLabel = [[UILabel alloc] initWithFrame:CGRectMake(allImageView.frame.origin.x+allImageView.frame.size.width+5, allImageView.frame.origin.y-2, 50, 20)];
    allLabel.backgroundColor = [UIColor clearColor];
    allLabel.textColor = [UIColor whiteColor];
    allLabel.font = [UIFont systemFontOfSize:15];
    allLabel.text = @"全选";
    [jiesuanView addSubview: allLabel];
    
    UIButton *allSelectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    allSelectBtn.frame = CGRectMake(10, 13, 50, 20);
    [allSelectBtn addTarget:self action:@selector(allSelectedAction) forControlEvents:UIControlEventTouchUpInside];
    isAllSelected = YES;
    [jiesuanView addSubview:allSelectBtn];
    
    
    
    totalLabel = [[UILabel alloc] initWithFrame:CGRectMake(allLabel.frame.origin.x+allLabel.frame.size.width+5, allLabel.frame.origin.y, 130, 20)];
    totalLabel.backgroundColor = [UIColor clearColor];
    totalLabel.textColor = [UIColor whiteColor];
    totalLabel.text = @"总计:￥0";
    totalLabel.font = [UIFont systemFontOfSize:15];
    [jiesuanView addSubview: totalLabel];
    
    
    jiesuanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [jiesuanBtn setTitle:@"去结算(0)" forState:UIControlStateNormal];
    [jiesuanBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    jiesuanBtn.titleLabel.font = [UIFont systemFontOfSize:12.5];
    [jiesuanBtn setBackgroundImage:[UIImage imageNamed:@"goToCount_"] forState:UIControlStateNormal];
    jiesuanBtn.frame = CGRectMake(233, 10, 71, 26);
    [jiesuanBtn addTarget:self action:@selector(goToJieSuan) forControlEvents:UIControlEventTouchUpInside];
    
    [jiesuanView addSubview:jiesuanBtn];
    
    
    
    //初始化编辑view
    
    
    [window addSubview:toumingView];
    float editY = 170.0;
    
    if (!iPhone5) {
        editY = 70.0;
    }
    
    editView = [[UIImageView alloc] initWithFrame:CGRectMake(10, editY, 300, 140)];
    editView.userInteractionEnabled = YES;
    editView.image = [UIImage imageNamed:@"edit_view"];
    
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 20, 100, 15)];
    textLabel.backgroundColor = [UIColor clearColor];
    textLabel.text = @"修改数量：";
    [editView addSubview:textLabel];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(editView.frame.size.width-36, 3, 34,34);
    [cancelBtn setBackgroundImage:[UIImage imageNamed:@"edit_delete"] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelEditView) forControlEvents:UIControlEventTouchUpInside];
    [editView addSubview:cancelBtn];
    
    UIButton *minusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    minusBtn.frame = CGRectMake(132/2, 80,25, 25);
    [minusBtn setBackgroundImage:[UIImage imageNamed:@"edit_minus"] forState:UIControlStateNormal];
    [minusBtn addTarget:self action:@selector(minusNum) forControlEvents:UIControlEventTouchUpInside];
    [editView addSubview:minusBtn];
    
    
    
    
    numTf = [[UITextField alloc] initWithFrame:CGRectMake(minusBtn.frame.origin.x+minusBtn.frame.size.width+39/2, minusBtn.frame.origin.y-5, 80, 30)];
    numTf.backgroundColor = [UIColor clearColor];
    numTf.keyboardType = UIKeyboardTypeNumberPad;
    numTf.delegate = self;
    
    UIImageView *bacView = [[UIImageView alloc] initWithFrame:CGRectMake(minusBtn.frame.origin.x+minusBtn.frame.size.width+39/2-3,  minusBtn.frame.origin.y-5, 83, 30)];
    bacView.image = [UIImage imageNamed:@"edit_shuru"];
    
    [editView addSubview:bacView];
    
    [editView addSubview:numTf];
    
    [window addSubview:editView];
    
    toumingView.hidden = YES;
    editView.hidden = YES;
    
    
    
    UIButton *plusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    plusBtn.frame = CGRectMake(numTf.frame.size.width+numTf.frame.origin.x+39/2, minusBtn.frame.origin.y, 25, 25);
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"edit_add"] forState:UIControlStateNormal];
    [plusBtn addTarget:self action:@selector(plusNum) forControlEvents:UIControlEventTouchUpInside];
    [editView addSubview:plusBtn];
    
    
    moveView = [[UIView alloc] initWithFrame:CGRectMake(0, UI_SCREEN_HEIGHT, UI_SCREEN_WIDTH, 40)];
    moveView.backgroundColor = RGBA(44, 43, 42, 1.0);
    [window addSubview:moveView];
    
    
    UIButton *canBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    canBtn.frame = CGRectMake(15, 15, 44, 30);
    canBtn.titleLabel.font = [UIFont systemFontOfSize:18.5];
    [canBtn setTitle:@"取消" forState:UIControlStateNormal];
    [canBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [canBtn addTarget:self action:@selector(cancelEditView) forControlEvents:UIControlEventTouchUpInside];
    
    
    [moveView addSubview:canBtn];
    
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(moveView.frame.size.width-44-15, 15, 44, 30);
    [sureBtn setTitle:@"完成" forState:UIControlStateNormal];
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:18.5];
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
    [moveView addSubview:sureBtn];
    
}


#pragma UITableView  代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_dataArrary count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellIdentifer = @"trolleyCell";
    trolleyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
    if (!cell) {
        cell = [[trolleyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifer];
        [cell.toumingBtn addTarget:self action:@selector(selectShop:) forControlEvents:UIControlEventTouchUpInside];
        
        [cell.numBtm addTarget:self action:@selector(editNumber:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    if (indexPath.row == 0) {
        cell.bgImageView.image = [UIImage imageNamed:@"shop_top_cell"];
        cell.selectedImageView.image = [UIImage imageNamed:@"shop_top_selected"];
    }else{
        cell.bgImageView.image = [UIImage imageNamed:@"shop_mid_cell"];
        cell.selectedImageView.image = [UIImage imageNamed:@"shop_mid_selected"];
    }
    
    if ([_indexArrary containsObject:indexPath]) {
        cell.selectBtn.selected = YES;
    }else{
        cell.selectBtn.selected = NO;
    }
    cell.shopTrolley = [_dataArrary objectAtIndex:indexPath.row];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ShopTrolley *trolley = [_dataArrary objectAtIndex:indexPath.row];
    
//    goodDetailViewController * detail = [[goodDetailViewController alloc] init];
//    detail.goodsID = trolley.goodId;
//    [self.navigationController pushViewController:detail animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ShopTrolley *shop;
    if ([_dataArrary count] > 0) {
        shop = [_dataArrary objectAtIndex:indexPath.row];
        if (shop.attrHeight == 0.0) {
            
            return 140;
        }
    }
    
    
    return 140+5+shop.attrHeight;
}

//删除
-(void)deleteShop{
    //调用接口
    
    
    if ([_indexArrary count]==0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先选中一种商品" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }else{
        UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定删除吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        al.tag = 1000;
        [al show];
        
    }
    
    
    
    
}


//alert代理
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1&&alertView.tag == 1000) {
        [self deleteShopTrolleyListDatas];
    }
}
//选中
-(void)selectShop:(SmallButton *)btn{
    NSIndexPath *indexPath = [_shopTableview indexPathForCell:btn.smallCell];
    
    btn.smallCell.selectBtn.selected = !btn.smallCell.selectBtn.selected;
    if (btn.smallCell.selectBtn.selected) {
        
        [_indexArrary addObject:indexPath];
        
    }else{
        [_indexArrary removeObject:indexPath];
    }
    if (_indexArrary.count!=_dataArrary.count) {
        allImageView.image = [UIImage imageNamed:@"all_normal"];
        isAllSelected = NO;
    }else{
        allImageView.image = [UIImage imageNamed:@"all_selected"];
        isAllSelected = YES;
    }
    total = 0.0f;
    for (int i = 0; i<[_indexArrary count];i++) {
        NSIndexPath *pa = [_indexArrary objectAtIndex:i];
        ShopTrolley *tooley = [_dataArrary objectAtIndex:pa.row];
        
        total += [tooley.price floatValue]*[tooley.number floatValue];
        
        
        
    }
    
    totalLabel.text = [NSString stringWithFormat:@"总计：￥%.2f",total];
    [jiesuanBtn setTitle:[NSString stringWithFormat:@"去结算(%d)",[_indexArrary count]] forState:UIControlStateNormal];
}

//编辑数量
-(void)editNumber:(SmallButton *)btn{
    midBtn = btn;
    
    oldNum = numTf.text = btn.titleLabel.text;
    
    toumingView.hidden = NO;
    editView.hidden = NO;
    [numTf becomeFirstResponder];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)cancelEditView{
    toumingView.hidden = YES;
    editView.hidden = YES;
    [numTf resignFirstResponder];
}

-(void)sureAction{
    if([numTf.text intValue]>1000){
        UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"商品数量不能大于1000" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [aler show];
    }else{
        if (![oldNum isEqualToString:numTf.text]) {
            [self editShopTrolleyListDatas];
        }else{
            [self cancelEditView];
        }
    }
    
    
    
}
//减1
-(void)minusNum{
    int num = [numTf.text intValue];
    if (num > 1) {
        num--;
    }
    numTf.text = [NSString stringWithFormat:@"%d",num];
}
//加1
-(void)plusNum{
    int num = [numTf.text intValue];
    num++;
    numTf.text = [NSString stringWithFormat:@"%d",num];
}

//键盘的动作处理
-(void) keyboardWillShow:(NSNotification *)note{
    NSLog(@"keyboardWillShow");
    
    // get keyboard size and loctaion
	CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    // Need to translate the bounds to account for rotation.
    
	// get a rect for the textView frame
	// animations settings
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];
	
    moveView.frame = CGRectMake(0, UI_SCREEN_HEIGHT-266, UI_SCREEN_WIDTH, 50);
	// set views with new info
    
    
    
	// commit animations
	[UIView commitAnimations];
}

-(void) keyboardWillHide:(NSNotification *)note{
    NSLog(@"keyboardWillHide");
    
    
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
	// animations settings
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];
    
    
    moveView.frame = CGRectMake(0, UI_SCREEN_HEIGHT, UI_SCREEN_WIDTH, 50);
	// set views with new info
	
	// commit animations
	[UIView commitAnimations];
}


//全选
-(void)allSelectedAction{
    
    
    if (isAllSelected) {
        
        isAllSelected = NO;
        allImageView.image = [UIImage imageNamed:@"all_normal"];
        for (int i = 0; i<_dataArrary.count; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            trolleyCell *cell = (trolleyCell *)[_shopTableview cellForRowAtIndexPath:indexPath];
            cell.selectBtn.selected = NO;
            [_indexArrary removeAllObjects];
            
        }
        
        
    }else{
        isAllSelected = YES;
        allImageView.image = [UIImage imageNamed:@"all_selected"];
        for (int i = 0; i<_dataArrary.count; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            trolleyCell *cell = (trolleyCell *)[_shopTableview cellForRowAtIndexPath:indexPath];
            cell.selectBtn.selected = YES;
            
            [_indexArrary removeAllObjects];
            for (int i =0; i<[_dataArrary count]; i++) {
                NSIndexPath *pa = [NSIndexPath indexPathForRow:i inSection:0];
                [_indexArrary addObject:pa];
            }
            
        }
    }
    [self refreshFooterViews];
    [jiesuanBtn setTitle:[NSString stringWithFormat:@"去结算(%d)",[_indexArrary count]] forState:UIControlStateNormal];
}
//去结算
-(void)goToJieSuan{
    if ([_indexArrary count]==0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"至少选中一种商品" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }else{
        
        float totalW = 0.0;
        shopId = @"";
        WriteOrderViewController *writeVC = [[WriteOrderViewController alloc]init];
        writeVC.dataArr = [NSMutableArray arrayWithCapacity:3];
        for (NSIndexPath *path in _indexArrary) {
            ShopTrolley *tolley = [_dataArrary objectAtIndex:path.row];
            [writeVC.dataArr addObject:tolley];
            totalW+= [tolley.weight floatValue];
            shopId = [shopId stringByAppendingString:[NSString stringWithFormat:@",%@",tolley.shopId]];
            
        }
        
        writeVC.totalWidth = totalW;
        writeVC.shopId = shopId;
        writeVC.shopVC = self;
        writeVC.totalAmount = total;
        [self.navigationController pushViewController:writeVC animated:YES];
    }
}

-(void)backPrePage{
    
    
    [self.navigationController popViewControllerAnimated:YES];
}

//获取购物车数据

- (void)getShopTrolleyListDatas{
    NSString * uid = [[[NSUserDefaults standardUserDefaults] objectForKey:@"MYSELF_INFO"] objectForKey:@"uid"];
    
    uid = @"1281019";///假数据
    if ([XDTools NetworkReachable])
    {
        NSDictionary * dic = @{@"userID":[[uid base64] base64],
                               @"version":@"1.0"
                               };
        
        ASIHTTPRequest *request = [XDTools postECShopRequestWithDict:dic API:API_CARLIST sign:@"1"];
        __weak ASIHTTPRequest * mrequest = request;
        [request setCompletionBlock:^{
            
            [XDTools hideProgress:self.view];
            
            NSDictionary *tempDic = [XDTools  JSonFromString:[mrequest responseString]];
            DDLOG(@"temdic = %@",tempDic);
            if ([[tempDic objectForKey:@"status"] intValue] == 1)
            {
                [_dataArrary removeAllObjects];
                
                if (isAllSelected) {
                    [_indexArrary removeAllObjects];
                }
                
                total = 0;;
                
                NSArray *goods = [tempDic objectForKey:@"data"];
                for (int i = 0; i<[goods count];i++) {
                    ShopTrolley *tooley = [[ShopTrolley alloc] initWithDic:[goods objectAtIndex:i]];
                    [_dataArrary addObject:tooley];
                    
                    NSIndexPath *pa = [NSIndexPath indexPathForRow:i inSection:0];
                    if (isAllSelected) {
                        [_indexArrary addObject:pa];
                    }
                    
                    
                    total += [tooley.price floatValue]*[tooley.number floatValue];
                    
                    
                    
                }
                if ([_dataArrary count] == 0) {
                    noDatasView.hidden = NO;
                    jiesuanView.hidden = YES;
                    rightButton.hidden = YES;
                }else{
                    noDatasView.hidden = YES;
                    jiesuanView.hidden = NO;
                    rightButton.hidden = NO;
                }
                if (_indexArrary.count == _dataArrary.count) {
                    isAllSelected = NO;
                    [self allSelectedAction];
                }
                totalLabel.text = [NSString stringWithFormat:@"总计：￥%.2f",total];
                [jiesuanBtn setTitle:[NSString stringWithFormat:@"去结算(%d)",[_indexArrary count]] forState:UIControlStateNormal];
                [_shopTableview reloadData];
            }
            else
            {
                [XDTools showTips:[tempDic objectForKey:@"msg"] toView:self.view];
            }
        }];
        
        [request setFailedBlock:^{
            [XDTools hideProgress:self.view];
            NSError *error = [mrequest error];
            DDLOG_CURRENT_METHOD;
            DDLOG(@"error=%@",error);
            if (error.code == 2) {
                [XDTools showTips:@"网络请求超时" toView:self.view];
            }
        }];
        
        [request startAsynchronous];
        
    }
    else
    {
        [XDTools showTips:brokenNetwork toView:self.view];
    }
}

//编辑购物车数据

- (void)editShopTrolleyListDatas{
    NSString * uid = [[[NSUserDefaults standardUserDefaults] objectForKey:@"MYSELF_INFO"] objectForKey:@"uid"];
    
     uid = @"1281019";///假数据
    trolleyCell *cell = midBtn.smallCell;
    if ([XDTools NetworkReachable])
    {
        NSDictionary * dic = @{@"goodsNumber":[[numTf.text base64] base64],
                               @"recID":[[cell.shopTrolley.shopId base64] base64],
                               @"userID":[[uid base64] base64],
                               @"version":@"1.0"
                               };
        
        ASIHTTPRequest *request = [XDTools postECShopRequestWithDict:dic API:API_EDITCARLIST sign:@"1"];
        __weak ASIHTTPRequest * mrequest = request;
        [request setCompletionBlock:^{
            
            [XDTools hideProgress:self.view];
            
            NSDictionary *tempDic = [XDTools  JSonFromString:[mrequest responseString]];
            DDLOG(@"temdic = %@",tempDic);
            if ([[tempDic objectForKey:@"status"] intValue] == 1)
            {
                [self cancelEditView];
                [self getShopTrolleyListDatas];
                
                
                
            }else{
                [XDTools showTips:[tempDic objectForKey:@"msg"] toView:self.view];
                [self cancelEditView];
            }
            
            
        }];
        
        [request setFailedBlock:^{
            [XDTools hideProgress:self.view];
            NSError *error = [mrequest error];
            DDLOG_CURRENT_METHOD;
            DDLOG(@"error=%@",error);
            if (mrequest.error.code == 2) {
                [XDTools showTips:@"网络请求超时" toView:self.view];
            }
        }];
        
        [request startAsynchronous];
        
    }
    else
    {
        [XDTools showTips:brokenNetwork toView:self.view];
    }
}

//删除购物车数据

- (void)deleteShopTrolleyListDatas{
    NSString * uid = [[[NSUserDefaults standardUserDefaults] objectForKey:@"MYSELF_INFO"] objectForKey:@"uid"];
    
     uid = @"1281019";///假数据
    
    
    shopId = @"";
    for (NSIndexPath *path in _indexArrary) {
        ShopTrolley *tolley = [_dataArrary objectAtIndex:path.row];
        shopId = [shopId stringByAppendingString:[NSString stringWithFormat:@",%@",tolley.shopId]];
    }
    if ([XDTools NetworkReachable])
    {
        NSDictionary * dic = @{@"recID":[[shopId base64] base64],
                               @"userID":[[uid base64] base64],
                               @"version":@"1.0"
                               };
        
        ASIHTTPRequest *request = [XDTools postECShopRequestWithDict:dic API:API_DELCARLIST sign:@"1"];
        __weak ASIHTTPRequest * mrequest = request;
        [request setCompletionBlock:^{
            
            [XDTools hideProgress:self.view];
            
            NSDictionary *tempDic = [XDTools  JSonFromString:[mrequest responseString]];
            DDLOG(@"temdic = %@",tempDic);
            if ([[tempDic objectForKey:@"status"] intValue] == 1)
            {
                
                if ([_indexArrary count] >0) {
                    for (int i = 0 ; i<[_indexArrary count] ;i++  ){
                        NSIndexPath *path = [_indexArrary objectAtIndex:0];
                        trolleyCell *cell = (trolleyCell *)[_shopTableview cellForRowAtIndexPath:path];
                        [_dataArrary removeObject:cell.shopTrolley];
                        
                        [_shopTableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:path] withRowAnimation:UITableViewRowAnimationRight];
                    }
                    
                    [_indexArrary removeAllObjects];
                    [_shopTableview reloadData];
                    
                    if ([_dataArrary count] == 0) {
                        noDatasView.hidden = NO;
                        jiesuanView.hidden = YES;
                        rightButton.hidden = YES;
                    }else{
                        noDatasView.hidden = YES;
                        jiesuanView.hidden = NO;
                        rightButton.hidden = NO;
                    }
                }
                [self refreshFooterViews];
                
            }else{
                [XDTools showTips:[tempDic objectForKey:@"msg"] toView:self.view];
            }
            
            
        }];
        
        [request setFailedBlock:^{
            [XDTools hideProgress:self.view];
            NSError *error = [mrequest error];
            DDLOG_CURRENT_METHOD;
            DDLOG(@"error=%@",error);
            if (mrequest.error.code == 2) {
                [XDTools showTips:@"网络请求超时" toView:self.view];
            }
        }];
        
        [request startAsynchronous];
        
    }
    else
    {
        [XDTools showTips:brokenNetwork toView:self.view];
    }
}


-(void)refreshFooterViews{
    total = 0.0f;
    for (int i = 0; i<[_indexArrary count];i++) {
        NSIndexPath *pa = [_indexArrary objectAtIndex:i];
        ShopTrolley *tooley = [_dataArrary objectAtIndex:pa.row];
        
        total += [tooley.price floatValue]*[tooley.number floatValue];
        
        
        
    }
    
    totalLabel.text = [NSString stringWithFormat:@"总计：￥%.2f",total];
    [jiesuanBtn setTitle:[NSString stringWithFormat:@"去结算(%d)",[_indexArrary count]] forState:UIControlStateNormal];
}



@end
